package com.douhulu.bytedance.open.util.json;

import com.douhulu.bytedance.common.util.json.GsonHelper;
import com.douhulu.bytedance.open.bean.ByteOpenAccessToken;
import com.google.gson.*;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Type;

/**
 * <h2> 奕泽云 -  </h2>
 *
 * @Author: 奕泽云 - 曾庆雷
 * @Email: 75881401@qq.com
 * @Since: 2022/5/1  20:45
 * CopyRight：山西威斯汀文化传播有限公司
 */
@Slf4j
public class ByteOpenAccessTokenGsonAdapter implements JsonDeserializer<ByteOpenAccessToken> {

    @Override
    public ByteOpenAccessToken deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
        ByteOpenAccessToken authorizerAccessToken = new ByteOpenAccessToken();
        JsonObject jsonObject = jsonElement.getAsJsonObject().getAsJsonObject("data");
        authorizerAccessToken.setErrorCode(GsonHelper.getInteger(jsonObject, "error_code"));
        authorizerAccessToken.setAccessToken(GsonHelper.getString(jsonObject, "access_token"));
        authorizerAccessToken.setRefreshToken(GsonHelper.getString(jsonObject, "refresh_token"));
        authorizerAccessToken.setExpiresIn(GsonHelper.getPrimitiveInteger(jsonObject, "expires_in"));
        authorizerAccessToken.setOpenId(GsonHelper.getString(jsonObject, "open_id"));
        authorizerAccessToken.setCaptcha(GsonHelper.getString(jsonObject, "captcha"));
        authorizerAccessToken.setDescription(GsonHelper.getString(jsonObject, "description"));
        authorizerAccessToken.setDescUrl(GsonHelper.getString(jsonObject, "desc_url"));
        authorizerAccessToken.setRefreshExpiresIn(GsonHelper.getPrimitiveInteger(jsonObject, "refresh_expires_in"));
        authorizerAccessToken.setScope(GsonHelper.getString(jsonObject, "scope"));
        authorizerAccessToken.setLogId(GsonHelper.getString(jsonObject, "log_id"));
        return authorizerAccessToken;
    }
}
